PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

Concatenation

The concatenation operator (&) can handle operands of any class. The result type of a concatenation depends on the type of the left hand operand. If the left-hand operand is a string, the result is always a string, and only in this case does AppleScript try to coerce the value of the right-hand operand to match that of the left. If the left-hand operand is a record, the result is always a record. If the left-hand operand is any other type, the result is a list.

Table 6-1, AppleScript operators summarizes the concatenation operator (&) and other AppleScript operators.

STRING

The concatenation of two strings is a string that begins with the characters in the string to the left of the operator, followed immediately by the characters in the string to the right of the operator. AppleScript does not add spaces or other characters between the two strings. For example,

"dump" & "truck"

returns the string "dumptruck" .

If the operand to the left of the operator is a string, but the operand to the right is not, AppleScript attempts to coerce the operand to the right to a string. For example, when AppleScript evaluates the expression

"Route " & 66

it coerces the integer 66 to the string "66" , and the result is

"Route 66"

However, you get a different result if you reverse the order of the operands:

66 & "Route "
--result: {66, "Route "}
RECORD

The concatenation of two records is a record that begins with the properties of the record to the left of the operator, followed by the properties of the record to the right of the operator. If both records contain properties with the same name, the value of the property from the record to the left of the operator appears in the result. For example, the result of the expression

{ name:"Matt", mileage:"8000" } & ¬
    { name:"Steve", framesize:58 }

is

{ name:"Matt", mileage:"8000", frameSize:58 }
ALL OTHER CLASSES

The concatenation of two operands that are not strings or records is a list whose first item is the value of the operand to the left of the operator, and whose second item is the value of the operand to the right of the operator. If the operands to be concatenated are lists, then the result is a list containing all the items in the list to the left of the operator, followed by all the items in the list to the right of the operator. For example,

{ "This" } & { "and", "that" }
--result: { "This", "and", "that" }
{ "This" } & item 1 of { "and", "that" }
--result: { "This", "and" }

In the following example, however, both specified items are strings, so concatenation results in a string:

item 1 of { "This" } & item 1 of { "and", "that" }
--result: "Thisand"

For large lists, it may be more efficient to use the Copy or Set command, rather than concatenation, to add an item to a list. For information on working efficiently with large lists, see List.


© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)